
//
//  RefoundDetailViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/5/31.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "RefoundDetailViewController.h"
#import "UserInfoModel.h"
#import "RefundInfoModel.h"
#import "AllOrdersListViewController.h"
#import "LGRefoundDetailAmountCell.h"
#import "LGRefoundDetailCauseCell.h"
#import "LGRefoundDetailImgVCell.h"
#import "IdAndMaOrderListViewController.h"
#import "MessageListViewController.h"
NSString * const  LGRefoundDetailAmountCellIdentifier = @"LGRefoundDetailAmountCellIdentifier";
NSString * const  LGRefoundDetailCauseCellIdentifier = @"LGRefoundDetailCauseCellIdentifier";
NSString * const  LGRefoundDetailImgVCellIdentifier = @"LGRefoundDetailImgVCellIdentifier";
@interface RefoundDetailViewController ()
@property(nonatomic, assign) RefoundDetailVCStyle style;
@property(nonatomic, assign) RefoundDetailVCType type;
@property(nonatomic, copy) NSString * orderId;
@property(nonatomic, strong)  RefundInfoModel * model;
@end

@implementation RefoundDetailViewController
- (instancetype)initWithOrderId:(NSString *) orderId withStyle:(RefoundDetailVCStyle )style withType:(RefoundDetailVCType)type{
    self = [super init];
    if (self) {
        self.style = style;
        self.orderId = orderId;
        self.type = type;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationItemTitle:@"退款详情"];
    if (self.style == RefoundDetailVCStyle_apply) {
        [self setupRightItem];
    }
    [self requestRefundInfo];
 
}
- (void)registerCell{
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LGRefoundDetailAmountCell class]) bundle:nil] forCellWithReuseIdentifier:LGRefoundDetailAmountCellIdentifier];
     [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LGRefoundDetailCauseCell class]) bundle:nil] forCellWithReuseIdentifier:LGRefoundDetailCauseCellIdentifier];
     [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LGRefoundDetailImgVCell class]) bundle:nil] forCellWithReuseIdentifier:LGRefoundDetailImgVCellIdentifier];
}
- (void)requestRefundInfo{
   
        NSDictionary * params = @{@"order_id":self.orderId,@"token":UserInfo.userToken};
    
    NSString* pathStr = @"";
    if (self.type == RefoundDetailVCType_order) {
        pathStr = @"/Order/refundInfo";
    }else if(self.type == RefoundDetailVCType_idOrMatn){
        pathStr= @"Otherorder/refundInfo";
    }
        WEAKSELF
        [[MainApi sharedInstance] postPath:pathStr params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
            NSLog(@"%@===",responseObject);
            STRONGSELF
            if (!error) {
                strongSelf.model = [[RefundInfoModel alloc]initWithDictionary:responseObject error:nil];
                
            }
            [self.collectionView reloadData];
        }];
}
- (void)setupRightItem{
    
    NSString * barTitle = @"取消退款";
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:barTitle forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)rightAction:(id)sender{
    
    [self requestCancelRefund];
}


#pragma mark ---
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger rows = 0;
    if (section == 0) {
        rows = 4;
    }else {
        rows = [self.model.data.refund_img count];
       
    }
    return rows;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    if (indexPath.section == 0) {
        cell = [self getTitleCollectionView:collectionView withIndexPath:indexPath];
    }else{
        cell = [self getImgVCollectionView:collectionView withIndexPath:indexPath];
    }
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (UICollectionViewCell *)getTitleCollectionView:(UICollectionView *)collectionView withIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = nil;
    if (indexPath.row == 1) {
        LGRefoundDetailAmountCell * tempCell =  [collectionView dequeueReusableCellWithReuseIdentifier:LGRefoundDetailAmountCellIdentifier forIndexPath:indexPath];
        NSString * titleStr = [@"退款金额：" stringByAppendingString:self.model.data.refund_price ? self.model.data.refund_price: @""];
        [tempCell setupAmount:titleStr];
        cell = tempCell;
    }else{
        LGRefoundDetailCauseCell * tempCell =  [collectionView dequeueReusableCellWithReuseIdentifier:LGRefoundDetailCauseCellIdentifier forIndexPath:indexPath];
        NSString * titleStr = @"";
        BOOL hiddenLine = NO;
        if (indexPath.row == 0) {
            titleStr = [@"退款原因：" stringByAppendingString:self.model.data.refund_desc? self.model.data.refund_desc: @""];
        }else if (indexPath.row == 2){
              titleStr = [@"退款说明：" stringByAppendingString:self.model.data.refund_explain ? self.model.data.refund_explain: @""];
        }else if (indexPath.row == 3){
            titleStr = @"凭证";
            hiddenLine = YES;
        }
        [tempCell setupTitle:titleStr withLineHidden:hiddenLine];
        cell = tempCell;
    }
     return cell;
}
- (UICollectionViewCell *)getImgVCollectionView:(UICollectionView *)collectionView withIndexPath:(NSIndexPath *)indexPath{
    LGRefoundDetailImgVCell* tempCell =  [collectionView dequeueReusableCellWithReuseIdentifier:LGRefoundDetailImgVCellIdentifier forIndexPath:indexPath];
    [tempCell setupImg:self.model.data.refund_img[indexPath.row]];
    return tempCell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize size =  CGSizeZero;
    if (indexPath.section == 0) {
        size = CGSizeMake(self.view.frame.size.width, 50);
    }else
        size = CGSizeMake(80, 80);
    return size;
}
#pragma mark =-----

//取消退款
- (void)requestCancelRefund{
    if (!self.model.data.refund_id) {
        return ;
    }
    NSDictionary * params = @{@"refund_id":self.model.data.refund_id,@"token":UserInfo.userToken};
    
    NSString* pathStr = @"";
    if (self.type == RefoundDetailVCType_order) {
        pathStr = @"/Order/cancelRefund";
    }else if(self.type == RefoundDetailVCType_idOrMatn){
        pathStr= @"Otherorder/cancelRefund";
    }
    [[MainApi sharedInstance] postPath:pathStr params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        
        NSLog(@"%@===",responseObject);
        if (!error) {
             NSString * NotificationName = @"";
            if (self.fromVCType == RefoundDetailFromVCType_Msg) {
                  NotificationName = @"UPDATE_MESSAGE_LIST";
            }else{
               
                if ( self.type == RefoundDetailVCType_idOrMatn) {
                    NotificationName = @"UPDATE_IdAndMaOrder_LIST";
                }else if ( self.type == RefoundDetailVCType_order) {
                    NotificationName = @"UPDATE_ORDER_LIST";
                }
                
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationName object:nil];
            
            for (UIViewController * tempVC in self.navigationController.viewControllers) {
                if ([tempVC isKindOfClass:[AllOrdersListViewController class]]) {
                    [self.navigationController popToViewController:tempVC animated:YES];
                }
                if ([tempVC isKindOfClass:[IdAndMaOrderListViewController class]]) {
                    [self.navigationController popToViewController:tempVC animated:YES];
                }
                if ([tempVC isKindOfClass:[MessageListViewController class]]) {
                    [self.navigationController popToViewController:tempVC animated:YES];
                }
            }
            
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
